<?php
ini_set('user_agent', $_SERVER['HTTP_USER_AGENT']);
ini_set('default_charset', 'UTF-8');
ini_set('error_reporting', E_ERROR);
ini_set('display_errors', true);
ini_set('max_execution_time', 300);
ini_set('memory_limit', '512M');
ini_set('max_file_uploads', 100);
date_default_timezone_set('Europe/Berlin');
$version = '2.3.2';

global $error, $message; //Lisette (for popups)

$parsed_file = parse_url("http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'], PHP_URL_PATH);
$parsed_file = str_replace(basename($parsed_file),'', $parsed_file);
$url = "http://".$_SERVER['HTTP_HOST'].$parsed_file;
if (substr($url, -1) != "/") $url = $url."/";

$library_path = dirname(__FILE__).DIRECTORY_SEPARATOR.'library'.DIRECTORY_SEPARATOR;
$database_path = $library_path.'database'.DIRECTORY_SEPARATOR;
$usersdatabase_path = $library_path.'database'.DIRECTORY_SEPARATOR;


//Added BIN path
$bin_path = dirname(__FILE__).DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR;

if (!is_writable($library_path)) {
    die('Error! Directory: "'.$library_path.'" must be writable. Instructions are available <a href="http://www.bioinformatics.org/librarian/installation.php" target="_top">here</a>.');
}

### PHP 5.1 support (obsolete) ###
if (!function_exists('sys_get_temp_dir')) {
	function sys_get_temp_dir() {
		$tmpfile = tempnam(":\n\\/?><", "");
		$path = dirname($tmpfile);
		return $path;
  	}
}

$temp_dir = sys_get_temp_dir();
if (substr($temp_dir, -1) == DIRECTORY_SEPARATOR) $temp_dir = substr($temp_dir, 0, -1);

if (get_magic_quotes_gpc() == 1) {
	if(!empty($_POST)) {
		while (list($key, $value) = each($_POST)) {
			if (is_string($_POST[$key])) {
				if ($key != stripslashes($key)) unset($_POST[$key]);
				$_POST[stripslashes($key)] = stripslashes($value);
			}
			if (is_array($_POST[$key])) {
				while (list($key2, $value2) = each($_POST[$key])) {
					if ($key2 != stripslashes($key2)) unset($_POST[$key][$key2]);
					$_POST[$key][stripslashes($key2)] = stripslashes($value2);
				}
				if ($key != stripslashes($key)) {
					$_POST[stripslashes($key)] = $_POST[$key];
					unset($_POST[$key]);
				}
				reset($_POST[$key]);
			}
		}
		reset($_POST);
	}
	if(!empty($_GET)) {
		while (list($key, $value) = each($_GET)) {
			if (is_string($_GET[$key])) {
				if ($key != stripslashes($key)) unset($_GET[$key]);
				$_GET[stripslashes($key)] = stripslashes($value);
			}
			if (is_array($_GET[$key])) {
				while (list($key2, $value2) = each($_GET[$key])) {
					if ($key2 != stripslashes($key2)) unset($_GET[$key][$key2]);
					$_GET[$key][stripslashes($key2)] = stripslashes($value2);
				}
				if ($key != stripslashes($key)) {
					$_GET[stripslashes($key)] = $_GET[$key];
					unset($_GET[$key]);
				}
				reset($_GET[$key]);
			}
		}
		reset($_GET);
	}
}

### permanent sessions and garbage collection ###
### session garbage collection ###
$probability = rand(1,1000000);
if($probability == 500000) {
	$session_dir = $temp_dir.DIRECTORY_SEPARATOR.'I,_Librarian_sessions';
	if(is_dir($session_dir)) {
		$clean_files = @scandir($session_dir);
		foreach($clean_files as $clean_file) {
			if(time() - filemtime($session_dir.DIRECTORY_SEPARATOR.$clean_file) > 31536000)
			@unlink($session_dir.DIRECTORY_SEPARATOR.$clean_file);
		}
	}
}
### session garbage collection ###
if (!is_dir($temp_dir.DIRECTORY_SEPARATOR.'I,_Librarian_sessions')) mkdir($temp_dir.DIRECTORY_SEPARATOR.'I,_Librarian_sessions');
session_set_cookie_params(31536000);
session_save_path($temp_dir.DIRECTORY_SEPARATOR.'I,_Librarian_sessions');
session_start();
### permanent sessions and garbage collection ###
?>
